import 'package:wanandroid_flutter/utils/SPrefsUtil.dart';

import '../bean/user_info_entity.dart';
import '../network/request_util.dart';
import 'log_util.dart';

class User {
  User._internal();

  static final User _singleton = User._internal();

  factory User() => _singleton;

  Future<bool> isLogin() {
   return SPrefsUtil().isLogin.getValue();
  }

  Future<String> userName() {
   return SPrefsUtil().userName.getValue();
  }

  Future<int> userCoinCount() {
   return SPrefsUtil().userCoinCount.getValue();
  }

  loginSuccess(UserInfoEntity userInfoEntity) {
    try {
      SPrefsUtil().isLogin.setValue(true);
      SPrefsUtil().userName.setValue(userInfoEntity.nickname.toString());
      SPrefsUtil().userCoinCount.setValue(userInfoEntity.coinCount ?? 0);
    } catch (e) {
      WanLog.e("save user info to local error- $e");
    }
  }

  logout() {
    HttpGo.instance.cookieJar?.deleteAll();
    try {
      SPrefsUtil().isLogin.setValue(false);
      SPrefsUtil().userName.setValue("");
      SPrefsUtil().userCoinCount.setValue(0);
    } catch (e) {
      WanLog.e("logout user info error- $e");
    }
  }
}
